import pygame
import random

SCREEN_SIZE = (480, 700)
TITLE = '飞机大战'


class GameSprite(pygame.sprite.Sprite):
    def __init__(self, filename, speed=1):
        super().__init__()
        self.image = pygame.image.load(filename)
        self.rect = self.image.get_rect()
        self.speed = speed

    def update(self, *args):
        self.rect.y += self.speed

class Enemy(GameSprite):
    """敌方飞机类
    """
    def __init__(self):
        super().__init__("./image/enemy1.png", 3)
        self.rect.x = random.randint(0, 480-self.rect.w)

    def update(self, *args):
        super().update()
        if self.rect.y > SCREEN_SIZE[1]:
            self.kill()


class Bullet(GameSprite):
    def __init__(self):
        super().__init__("./image/bullet1.png", 5)

    def update(self, *args):
        self.rect.y -= self.speed
        if self.rect.y < 0:
            self.kill()


class BgSprite(GameSprite):
    def __init__(self, is_alt = False):
        super().__init__('./image/bg.png', 1)
        if is_alt:
            self.rect.y = -self.rect.height

    def update(self, *args):
        super().update()
        if self.rect.y > self.rect.height:
            self.rect.y = -self.rect.height


class Plane(GameSprite):
    def __init__(self):
        super().__init__("./image/me1.png")
        # self.image = pygame.image.load("./image/me1.png")
        # self.rect = self.image.get_rect()
        self.rect.midbottom = [SCREEN_SIZE[0]//2, SCREEN_SIZE[1]-10]
        self.bullet_group = pygame.sprite.Group()


    def update(self):
        pass

    def fire(self):
        bullet = Bullet()
        bullet.rect.centerx = self.rect.centerx
        bullet.rect.y = self.rect.y
        self.bullet_group.add(bullet)


class PlaneGame:

    def __init__(self):
        pygame.init()
        self.screen = pygame.display.set_mode(SCREEN_SIZE)
        pygame.display.set_caption(TITLE)
        self.is_loop = True
        self.clock = pygame.time.Clock()
        pygame.time.set_timer(pygame.USEREVENT, 3000)
        self.__create_sprites()

    def start(self):
        """开始游戏"""
        # y=0
        while self.is_loop:
            self.__event_handler()
            self.__update_sprites()
            self.clock.tick(40)
            pygame.display.update()
        pygame.quit()

    def __event_handler(self):
        """事件处理"""
        for e in pygame.event.get():
            if e.type == pygame.QUIT:
                self.is_loop = False
            if e.type == pygame.KEYDOWN:
                if e.key == pygame.K_LEFT:
                    self.move(-10)
                elif e.key == pygame.K_RIGHT:
                    self.move(10)
                elif e.key == pygame.K_SPACE:
                    self.plane.fire()
            elif e.type == pygame.USEREVENT:
                enemy = Enemy()
                self.enemy_group.add(enemy)

    def __create_sprites(self):
        """创建精灵"""
        bg1 = BgSprite()
        bg2 = BgSprite(True)
        self.plane = Plane()
        enemy = Enemy()
        self.bg_group = pygame.sprite.Group(bg1, bg2)
        self.plane_group = pygame.sprite.Group(self.plane)
        self.enemy_group = pygame.sprite.Group(enemy)

    def __update_sprites(self):
        """更新精灵"""
        self.bg_group.update()
        self.bg_group.draw(self.screen)
        self.plane_group.draw(self.screen)
        self.enemy_group.update()
        # print(len(self.enemy_group))
        self.enemy_group.draw(self.screen)
        self.plane.bullet_group.update()
        self.plane.bullet_group.draw(self.screen)
        mask_spirtes = pygame.sprite.spritecollide(self.plane, self.enemy_group, False)
        if len(mask_spirtes)>0:
            print('Game Over')
        pygame.sprite.groupcollide(self.enemy_group, self.plane.bullet_group, True, True)
    def move(self, v):
        self.plane.rect.x += v
        if self.plane.rect.x < 0:
            self.plane.rect.x = 0
        elif self.plane.rect.x > SCREEN_SIZE[0] - self.plane.rect.w:
            self.plane.rect.x = SCREEN_SIZE[0] - self.plane.rect.w




if __name__ == '__main__':
    plane = PlaneGame()
    plane.start()